package com.weaver.drools.common.utils;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;

import com.fasterxml.jackson.databind.ObjectMapper;

/**
 * 
 *   接口请求util
 *  @application name:
 *  @author: zhouxinlei 
 *  @time：2018年8月13日
 *  @version：ver 1.1
 */
@SuppressWarnings({ "rawtypes", "unchecked", "resource","deprecation" })
public class HttpTools {
	
	static final String apiPath = "/";
	/**
	 * @param path 路径
	 * @param typeList Map key
	 * @return Map
	 */
	public Map<String,List<Map>> dataTransterMap(String path,String typeList){
		Map<String,List<Map>> map = new HashMap<String,List<Map>>();
		StringBuilder sb = new StringBuilder();
		try {
			HttpClient httpclient = new DefaultHttpClient();
			//访问接口地址
			HttpGet httpgets = new HttpGet(apiPath + path);  
		    HttpResponse response = httpclient.execute(httpgets);    
		    HttpEntity entity= response.getEntity(); 
		      
			if (entity != null) {  
				InputStream is = null;
		        String line = null;      
		        try { 
		        	is = entity.getContent();
		        	BufferedReader reader = new BufferedReader(new InputStreamReader(is,"Utf-8"));  
		            while ((line = reader.readLine()) != null) {  
		                sb.append(line + "\n");      
		            }      
		        } catch (IOException e) {      
		            e.printStackTrace();      
		        } finally {      
		            try {      
		                is.close();      
		            } catch (IOException e) {      
		               e.printStackTrace();      
		            }      
		        }  
			}
			if(sb.toString().length() > 3){
				ObjectMapper mapper = new ObjectMapper();  
		        map = mapper.readValue("{\""+typeList+"\":"+sb.toString()+"}",Map.class);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return map;
	}
	/**
	 * 
	 * @param path 路径
	 * @return json数据
	 */
	public StringBuilder dataTransterJson(String path){
		
		StringBuilder sb = new StringBuilder();
		try {
			HttpClient httpclient = new DefaultHttpClient();
			//访问振义系统接口地址
			HttpGet httpgets = new HttpGet(apiPath + path);  
		    HttpResponse response = httpclient.execute(httpgets);    
		    HttpEntity entity= response.getEntity(); 
		      
			if (entity != null) {  
				InputStream is = null;
		        String line = null;      
		        try { 
		        	is = entity.getContent();
		        	BufferedReader reader = new BufferedReader(new InputStreamReader(is,"Utf-8"));  
		            while ((line = reader.readLine()) != null) {  
		                sb.append(line + "\n");      
		            }      
		        } catch (IOException e) {      
		            e.printStackTrace();      
		        } finally {      
		            try {      
		                is.close();      
		            } catch (IOException e) {      
		               e.printStackTrace();      
		            }      
		        }  
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return sb;
	}
}
